<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\manager\model\Manager;
use think\Cache;
use think\helper\Hash;
use think\Db;
use app\common\builder\ZBuilder;
use app\user\model\User as UserModel;

/**
 * 后台默认控制器
 * @package app\admin\controller
 */
class Index extends Admin
{
    /**
     * 后台首页
     * @author 蔡伟明 <314013107@qq.com>
     * @return string
     */
    public function index()
    {
        $admin_pass = Db::name('admin_user')->where('id', 1)->value('password');
        if (UID == 1 && $admin_pass && Hash::check('admin', $admin_pass)) {
            $this->assign('default_pass', 1);
        }
        $this->assign('role_index', ROLE_ID);
        return $this->fetch();
    }

    /**
     * 清空系统缓存
     * @author 蔡伟明 <314013107@qq.com>
     */
    public function wipeCache()
    {
        if (!empty(config('wipe_cache_type'))) {
            foreach (config('wipe_cache_type') as $item) {
                if ($item == 'LOG_PATH') {
                    $dirs = (array)glob(constant($item) . '*');
                    foreach ($dirs as $dir) {
                        array_map('unlink', glob($dir . '/*.log'));
                    }
                    array_map('rmdir', $dirs);
                } else {
                    array_map('unlink', glob(constant($item) . '/*.*'));
                }
            }
            Cache::clear();
            $this->success('清空成功');
        } else {
            $this->error('请在系统设置中选择需要清除的缓存类型');
        }
    }

    /**
     * 个人设置
     * @author 蔡伟明 <314013107@qq.com>
     */
    public function profile()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $data['nickname'] == '' && $this->error('昵称不能为空');
            $data['id'] = UID;
            // 如果没有填写密码，则不更新密码
            if ($data['password'] == '') {
                unset($data['password']);
            }
            //超级管理员
            if (ROLE_ID == 1):
                $UserModel = new UserModel();
                if ($user = $UserModel->allowField(['nickname', 'email', 'password', 'mobile', 'avatar'])->update($data)) {
                    // 记录行为
                    action_log('user_edit', 'admin_user', UID, UID, get_nickname(UID));
                    $this->success('编辑成功');
                } else {
                    $this->error('编辑失败');
                }
            //客户经理或则领导
            else:
                $UserModel = new UserModel();
                $Manager = new Manager();
                Db::startTrans();
                $data['tel_number'] = $data['mobile'];
                $user = $UserModel->allowField(['nickname', 'email', 'password', 'mobile', 'avatar'])->update($data);
                $data['id'] = Db::name('admin_user')->where('id',UID)->value('role_id');
                $manager = $Manager->allowField(['sex', 'tel_number', 'office_number', 'email'])->update($data);
                if (($user === false) || ($manager === false)):
                    Db::rollback();
                    $this->error("编辑失败");
                endif;
                Db::commit();
                // 记录行为
                action_log('user_edit', 'admin_user', UID, UID, get_nickname(UID));
                $this->success('编辑成功');
            endif;
        }
        //超级管理员
        if (ROLE_ID == 1):
            // 获取数据
            $info = UserModel::where('id', UID)->field('password', true)->find();

            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->addFormItems([ // 批量添加表单项
                    ['static', 'username', '用户名', '不可更改'],
                    ['text', 'nickname', '昵称', '可以是中文'],
                    ['text', 'email', '邮箱', ''],
                    ['password', 'password', '密码', '必填，6-20位'],
                    ['text', 'mobile', '手机号'],
                    ['image', 'avatar', '头像']
                ])
                ->setFormData($info)// 设置表单数据
                ->fetch();
        //客户经理或领导
        else:
            // 获取数据
            $a_fields = [
                'b.department', 'b.name', 'a.username', 'b.cert_number', 'a.nickname', 'b.sex sex',
                'b.tel_number mobile', 'b.office_number', 'a.avatar', 'b.email'
            ];
            $a_info = Db::name('admin_user a')
                ->field($a_fields)
                ->join('manager b', 'a.role_id=b.id')->where('a.id', UID)->find();
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
                ->addFormItems([ // 批量添加表单项
                    ['static', 'department', '部门', '不可更改'],
                    ['static', 'name', '姓名', '不可更改'],
                    ['static', 'username', '柜员号(登陆账号)', '不可更改'],
                    ['static', 'cert_number', '身份证号', '不可更改'],
                    ['text', 'nickname', '昵称', '可以是中文'],
                    ['password', 'password', '密码', '必填，6-20位'],
                    ['radio', 'sex', '性别', '', ['男' => '男', '女' => '女']],
                    ['text', 'mobile', '手机号'],
                    ['text', 'office_number', '办公电话'],
                    ['image', 'avatar', '头像'],
                    ['text', 'email', '邮箱', ''],
                ])
                ->setFormData($a_info)// 设置表单数据
                ->fetch();
        endif;
    }

    /**
     * 检查版本更新
     * @author 蔡伟明 <314013107@qq.com>
     * @return \think\response\Json
     */
    public function checkUpdate()
    {
        $params = config('dolphin');
        $params['domain'] = request()->domain();
        $params['website'] = config('web_site_title');
        $params['ip'] = $_SERVER['SERVER_ADDR'];
        $params['php_os'] = PHP_OS;
        $params['php_version'] = PHP_VERSION;
        $params['mysql_version'] = db()->query('select version() as version')[0]['version'];
        $params['server_software'] = $_SERVER['SERVER_SOFTWARE'];
        $params = http_build_query($params);

        $opts = [
            CURLOPT_TIMEOUT => 20,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL => config('dolphin.product_update'),
            CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $params
        ];

        // 初始化并执行curl请求
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($data, true);

        if ($result['code'] == 1) {
            return json([
                'update' => '<a class="badge badge-primary" href="http://www.dolphinphp.com/download" target="_blank">有新版本：' . $result["version"] . '</a>',
                'auth' => $result['auth']
            ]);
        } else {
            return json([
                'update' => '',
                'auth' => $result['auth']
            ]);
        }
    }
}